package com.example.onlinemusic.mapper;

import com.example.onlinemusic.model.Music;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface LoveMusicMapper {
    // 通过用户 id 来查询用户喜欢的音乐
    @Select("select * from lovemusic where user_id = #{userId} and music_id = #{musicId}")
    Music findLoveMusicByMusicIdAndUserId(int userId, int musicId);

    // 添加某个用户喜欢的音乐
    @Insert("insert into lovemusic (user_id, music_id) values (#{userId}, #{musicId})")
    boolean insertLoveMusic(int userId, int musicId);

    // 查询这个用户喜欢过的所有音乐
    @Select("select b.* from lovemusic as a,music as b where a.user_id = #{userId} and a.music_id = b.id")
    List<Music> findLoveMusicByUserId(int userId);

    // 查询当前用户指定为 title 的音乐,支持模糊查询
    @Select("select b.* from lovemusic as a,music as b where a.user_id = #{userId} and a.music_id = b.id and b.title like concat('%',#{title},'%')")
    List<Music> findLoveMusicByKeyAndUID(String title, int userId);

    // 取消某个用户喜欢的音乐
    @Delete("delete from lovemusic where user_id = #{userId} and music_id = #{musicId}")
    int deleteLoveMusic(int userId, int musicId);

    @Delete("delete from lovemusic where music_id = #{musicId}")
    int deleteLoveMusicByMusicId(int musicId);
}
